package cn.jinjimi.framework.router;

import android.content.Context;
import android.os.Bundle;

import cn.jinjimi.base.util.URLUtil;

import java.io.File;
import java.net.URI;
import java.util.Map;

/**
 * 属性
 * <p>
 *
 * @author jinsen
 * @date 15/9/15
 */
public class RouterOptions {
    private String targetUrl;
    private String originUrl;
    private Bundle bundle;

    private Context context;

    public RouterOptions(String url, Context context) {
        Bundle bundle = new Bundle();

        URI parsedUri = URLUtil.create(url);
        if (parsedUri == null) {
            this.setBundle(bundle);
            return;
        }

        String targetUrl = parsedUri.getHost() + parsedUri.getPath();
        if (targetUrl.endsWith(File.separator)) {
            targetUrl = targetUrl.substring(0, targetUrl.length() - 1);
        }

        Map<String, String> givenParams = RouterHelper.urlToParamsMap(url);


        for (Map.Entry<String, String> entry : givenParams.entrySet()) {
            bundle.putString(entry.getKey(), entry.getValue());
        }

        this.setContext(context);
        this.setOriginUrl(url);
        this.setTargetUrl(targetUrl);
        this.setBundle(bundle);
    }

    public void setBundle(Bundle bundle) {
        this.bundle = bundle;
    }

    public Bundle getBundle() {
        return this.bundle;
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public String getOriginUrl() {
        return originUrl;
    }

    public void setOriginUrl(String originUrl) {
        this.originUrl = originUrl;
    }

    public void setTargetUrl(String url) {
        this.targetUrl = url;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

}
